<?php
/**
* Tree Model Class
* 
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* 
* @copyright Copyright (c) 2008-2009 Ne8, LLC <josh.ribakoff@gmail.com>
* @license New BSD License
* @version 0.1
* @category Shuffler
* @package Shuffler_Model
* @subpackage Tree
*/
class Shuffler_Model_Tree extends Shuffler_Model implements Shuffler_Model_Tree_Interface
{
	/**
    * @var Shuffler_Model_Tree_Node_Interface
	 * Root node
	 */
	protected $root = null;

	/**
	 * Current working node
	 */
	protected $current = null;

	public function __construct()
	{
		$this->root = $this->newNode('ROOT');
		$this->current = $this->root;
        $this->init();
	}
    
    /**
    * Template method
    */
    public function init() { }
    
    /**
    * Create a new node in the tree
    * 
    * @param string
    * @return Shuffler_Model_Tree_Node_Interface
    */
    public function newNode( $name )
    {
        return new Shuffler_Model_Tree_Node( 0, $name );    
    }
    
    /**
    * Get the current node in the tree
    * 
    * @retrun Shuffler_Model_Tree_Node_Interface
    */
    public function getCurrent()
    {
        return $this->current;
    }
    
    /**
    * Set  the current node in the tree
    * 
    * @param Shuffler_Model_Tree_Node_Interface to set as the current node
    */
    public function setCurrent( Shuffler_Model_Tree_Node_Interface $node )
    {
        $this->current = $node;
        return $this;
    }

    /**
    * Add a child node
    * 
    * @param Shuffler_Model_Tree_Node_Interface to add
    * @bool optional $setCurrent | set this node as the current node so subsequent nodes become successors
    */
	public function addChild( Shuffler_Model_Tree_Node_Interface $node, $setCurrent = false)
	{
        if( !is_bool( $setCurrent ) )
        {
            throw new Exception( 'setCurrent must be boolean' );
        }
		$this->current->addChild( $node );
		if ($setCurrent)
        {
			$this->current = $node;
		}
        return $this;
	}

    /**
    * Get the parent node
    * 
    * @return Shuffler_Model_Tree_Node_Interface
    */
	public function getParent()
	{
		$this->current = $this->current->getParent();
	}

    /**
    * Reset the tree so the current node is set to the root node
    */
	public function reset()
	{
		$this->current = $this->root;
	}
    
    /**
    * Set the root of the tree to a given node
    * 
    * optionally sets the current tree node
    * to this new root node, enabled by default (opt-out functionality)
    * 
    * @param Shuffler_Model_Tree_Node_Interface new root node
    * @param boolean wether or not to set the new root node to this node, defaults functionality
    */
    public function setRoot( Shuffler_Model_Tree_Node_Interface $root, $setCurrent = true )
    {
        $this->root = $root;
        if( $setCurrent )
        {
            $this->current = $root;
        }
    }
    
    public function getRoot()
    {
        return $this->root;
    }

}